package com.smileymedia.view;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import com.smileymedia.R;
import com.smileymedia.core.Constants;
import com.smileymedia.smil.SmilRendererActivity;
import com.smileymedia.util.SDCardUtil;

import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import android.view.ViewGroup;

/**
 * List all the archived files in the system 
 * @author greg
 *
 */
public class ArchiveListActivity extends ListActivity{
	private final String TAG = ArchiveListActivity.class.getSimpleName();
	private ListAdapter mAdapter;
	private LayoutInflater mInflater;
	private ListView mList;
	
	
	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.archive);

		mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		List<String> strings = new ArrayList<String>();
		String[] files = SDCardUtil.listFiles(getBaseContext(), Constants.EXPORT_DIR, new FilenameFilter() {
			public boolean accept(File file, String fileName) {				
				return fileName.endsWith(".smil");
			}
		});

		for(String file:files){
			strings.add(file);
		}

		setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, strings) {
			public View getView(int position, View convertView, ViewGroup parent) {
				View row;
				if (null == convertView) {
					row = mInflater.inflate(R.layout.list_item, null);
				} else {
					row = convertView;
				}

				TextView tv = (TextView) row.findViewById(android.R.id.text1);
				tv.setText(getItem(position));

				return row;
			}
		});

		mList = (ListView) findViewById(android.R.id.list);

		mList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> parent, View view, int position,	long id) {
				String smilFile=(String) parent.getItemAtPosition(position);
								
				Intent intent = new Intent(getBaseContext(), SmilRendererActivity.class);
				Bundle bundle=new Bundle();
				bundle.putString("smil", smilFile);
				intent.putExtras(bundle);
				
				startActivity(intent);
				Log.d(TAG, "item="+smilFile);
				
			}

		});
		
	}
}
